<?php

namespace backend\controllers;

use Yii;
use services\models\minisite\MActivityShow;
use backend\models\MActivityShowSearch;

use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MActivityShowController implements the CRUD actions for MActivityShow model.
 */
class MActivityShowController extends MyController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }


    /**
     * Lists all MActivityShow models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new MActivityShowSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single MActivityShow model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new MActivityShow model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new MActivityShow();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Updates an existing MActivityShow model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['index', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing MActivityShow model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the MActivityShow model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return MActivityShow the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = MActivityShow::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    
    /**
     * ajax改变活动状态c_status【上线|下线】
     */
    public function actionChangeStatus()
    {  
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        
        $id = Yii::$app->request->post('id');
        if(intval($id) < 1) {
            return ['status'=>1,'msg'=>'缺少参数'];
        }
        $result = MActivityShow::findOne($id);
        
        if(empty($result)) {
            return ['status'=>2,'msg'=>'未查询到信息'];
        }
        
        $c_status = $result->c_status == 0 ? 1 : 0;
        
        //如果当前状态为下线状态，查询当前库里还有几个上线活动
        if($c_status == 0) {
            $count = MActivityShow::find()->where(['c_status'=>1])->count();
            if($count <= 1) {
                return ['status'=>4,'msg'=>'下线失败！活动不能为空！'];
            }
        }
        $result->c_status = $c_status;
        
        $result->save();
        
        return ['status'=>0,'msg'=>'修改成功','c_status'=>$c_status];
      
    }
}
